package com.czxy.controller;

import com.czxy.common.vo.Result;
import com.czxy.pojo.TCustomsPass;
import com.czxy.service.CustomsPassDetailService;
import com.czxy.service.CustomsPassService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author likk
 * @create 2018/9/5
 */
@RestController
@RequestMapping("/customsPass")
@Slf4j
public class CustomsPassController {

    @Autowired
    private CustomsPassService customsPassService;

    @RequestMapping("/findAll")
    public Result findAll(){
        try{

            List<TCustomsPass> list = customsPassService.findAll();

            return Result.ok(list);
        } catch (Exception e){
            e.printStackTrace();
            return Result.build(500, "查询失败！");
        }
    }

    /**
     * 查询关卡详情
     *
     * @param id
     * @return
     */
    @RequestMapping("findDetail/{id}")
    public Result customsPassService(@PathVariable("id") Integer id){
        try{

            // TODO 获取用户信息
            Integer userId = 1;

            TCustomsPass customsPass = customsPassService.findDetailById(id, userId);

            return Result.ok(customsPass);
        } catch (Exception e){
            e.printStackTrace();
            return Result.build(500, "查询失败！");
        }
    }

    /**
     * 新增关卡
     * @param customsPass
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result addUser(@RequestBody TCustomsPass customsPass) {

        try {
            customsPass.setStatus(1);
            customsPassService.saveCustomsPass(customsPass);

            // 保存关卡详情

            return Result.build(200, "新增成功！");
        } catch (Exception e) {
            log.error("新增关卡信息失败,错误信息：" + e.getMessage());

            return Result.build(500, "新增失败！");
        }

    }

    /**
     * 更新关卡信息
     * @param customsPass
     * @return
     */
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public Result update(@RequestBody TCustomsPass customsPass) {
        try {
            customsPassService.updateSelective(customsPass);

            return Result.build(200, "修改成功！");
        } catch (Exception e) {
            log.error("更新关卡信息失败,错误信息：" + e.getMessage());

            return Result.build(500, "修改失败！");
        }
    }

    /**
     * 逻辑删除关卡
     * @param id
     * @return
     */
    @RequestMapping(value = "delete/{id}",method = RequestMethod.POST)
    public  Result deleteUser(@PathVariable("id") Integer id) {
        try {
            TCustomsPass record = new TCustomsPass();
            record.setId(id);
            record.setStatus(0); //逻辑删除，设置为停用

            customsPassService.updateSelective(record);

            return Result.build(200, "删除成功！");
        } catch (Exception e) {
            log.error("删除关卡失败,错误信息：" + e.getMessage());

            return Result.build(500, "删除失败！");
        }
    }
}
